package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.ImMessage;
import com.sam.im.heiqun.entities.SearchUserEntity;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.adapters.SearchFriendAdpter;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.DividerItemDecorationUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 查找好友
 */
public class SeachFriendsActivity extends BaseSwipeBackActivity implements SearchFriendAdpter.SearchFriendListClickListener {

    //    @BindView(R.id.pre_v_back)
//    ImageView preVBack;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.friends_list)
    RecyclerView friendsList;
    @BindView(R.id.txt_clean)
    TextView txt_clean;
    @BindView(R.id.activity_seach_friends)
    LinearLayout activitySeachFriends;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.relative_noresult)
    LinearLayout relativeNoresult;
    private SearchFriendAdpter mSearchFriendAdpter;
    PGService mPgService;
    List<ImFriendEntivity> groupEntivities;

    @Override
    public int getContentViewId() {
        return R.layout.activity_seach_friends;
    }

    @Override
    protected String getTitleText() {
        return getString(R.string.add_friend);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mPgService = PGService.getInstance();
        Intent intent=getIntent();
        String et_String=intent.getStringExtra("str");
        if (et_String!=null){
            queryUser(et_String);
        }
        searchEdit.setText(et_String);

        mSearchFriendAdpter = new SearchFriendAdpter(this);
        friendsList.setLayoutManager(new LinearLayoutManager(this));
        mSearchFriendAdpter.setItemClickListener(this);
        friendsList.addItemDecoration(new DividerItemDecorationUtils(this, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.black_divider)));
        friendsList.setAdapter(mSearchFriendAdpter);
        groupEntivities = new ArrayList<>();
        searchEdit.setOnKeyListener(onKeyListener);

    }

    @Override
    protected void onResume() {
        super.onResume();
        ToolsUtils.popupKeyboard(searchEdit);
    }

    private View.OnKeyListener onKeyListener = new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                String str = searchEdit.getText().toString().trim();
//                    Log.i("-----", "onEditorAction: str="+str);
                if (null != str && !"".equals(str.toString())) {
                    queryUser(str.toString());
                } else {
//                        showToast("什么鬼啊");
                }
                return true;
            }
            return false;
        }
    };

    @Override
    protected void onDestroy() {
        //关闭软键盘
        ToolsUtils.closeKeyboard(this, searchEdit);
        super.onDestroy();
    }

    //    @OnClick({R.id.pre_v_back,R.id.txt_clean})
    @OnClick({R.id.txt_clean})
    public void onClick() {
        scrollToFinishActivity();
    }

    private void queryUser(final String mobile) {
        showProgress("");
//        String uid = ""+IMSharedPreferences.getsaveReadyEntivity().getMyInfo().getId();
        mPgService.queryUser(ToolsUtils.getMyUserId(), mobile).subscribe(new AbsAPICallback<SearchUserEntity>() {
            @Override
            public void onNext(SearchUserEntity friendEntivity) {
                hideProgress();
                groupEntivities = friendEntivity.getInfo();
                if (groupEntivities.size() == 0) {
                    friendsList.setVisibility(View.GONE);
                    relativeNoresult.setVisibility(View.VISIBLE);
                    tvContent.setText(mobile);
                } else {
                    friendsList.setVisibility(View.VISIBLE);
                    relativeNoresult.setVisibility(View.GONE);
                    mSearchFriendAdpter.refresh(groupEntivities);
                }
            }

            @Override
            protected void onResultError(ApiException ex) {
                hideProgress();
            }
        });
    }

    @Override
    public void onSearchItemClick(View view, int position) {
//        Log.i("-----", "--0001--");

        ImFriendEntivity imFriendEntivity = groupEntivities.get(position);
        UserEntivity userEntivity = ToolsUtils.getUser();
        if ((imFriendEntivity.getId() + "").equals(userEntivity.getId() + "")) {
            showToast(getResources().getString(R.string.conot_add_self_be_friend));
        } else {
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), FriendDetailActivity.class);
            intent.putExtra("id", imFriendEntivity.getId());
            intent.putExtra("type", 0);
            startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
